﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZX.HIS.IDAL;
using ZX.HIS.DBUtility;
using System.Collections.Generic;//Please add references

namespace ZX.HIS.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:DepartInfo
    /// </summary>
    public partial class DepartInfo : IDepartInfo
    {
        public DepartInfo()
        { }
        #region Depart自写方法

        /// <summary>
        /// 科室添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DepartInfolAdd(ZX.HIS.Model.DepartInfo model)
        {
            string str = "Proc_DepartInfolAdd";
            SqlParameter[] Parameter ={
                                      new SqlParameter("@DepartName",SqlDbType.VarChar){Value=model.DepartName},
                                      new SqlParameter("@HospitalId",SqlDbType.Int){Value=model.HospitalId}
                                      };
            object obj = SqlDBhelper.RunProcedureScalar(str, Parameter);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            else
            {
                return 0;
            }
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="StrOrder"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>
        public List<ZX.HIS.Model.DepartInfo> DepartInfoShow(int PageIndex, int PageSize, string DepartName, int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("SELECT * FROM (");
            str.AppendLine("SELECT * FROM(");
            str.AppendLine("SELECT ROW_NUMBER()OVER(");
            str.AppendLine("order by DepartId)as b,tbDepartInfo.* from tbDepartInfo");
            str.AppendLine("where DepartName like '%" + DepartName + "%' ");
            str.AppendLine(" AND HospitalId=" + HospitalId);
            str.AppendLine(" )TT");
            str.AppendLine("WHERE b  between  " + ((PageIndex - 1) * PageSize + 1) + " AND " + PageIndex * PageSize);
            str.AppendLine(")C");

            return SqlDBhelper.GetList<ZX.HIS.Model.DepartInfo>(str.ToString());

        }
        /// <summary>
        /// 科室表下拉框
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.DepartInfo> AllDepartInfo(int HospitalId)
        {
            string sql = "SELECT  *  FROM  tbDepartInfo WHERE HospitalId=" + HospitalId;
            return SqlDBhelper.GetList<ZX.HIS.Model.DepartInfo>(sql);

        }

        /// <summary>
        /// 查询总数量
        /// </summary>
        /// <param name="StrWher"></param>
        /// <returns></returns>
        public int DepartInfoCount(string DepartName, int HospitalId)
        {
            StringBuilder str = new StringBuilder();
            str.Append("SELECT COUNT(*) FROM tbDepartInfo ");
            str.AppendLine(" where DepartName like '%" + DepartName + "%' ");
            str.AppendLine(" AND HospitalId=" + HospitalId);
            return SqlDBhelper.ExecuteScalarInt(str.ToString());
        }

        public List<ZX.HIS.Model.DepartInfo> list()
        {
            string sql = "select * from tbDepartInfo";
            return SqlDBhelper.GetList<ZX.HIS.Model.DepartInfo>(sql);
        }

        /// <summary>
        /// 伪删除诊所
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="DeparStarte"></param>
        /// <returns></returns>
        public int DepartInfoDelById(ZX.HIS.Model.DepartInfo mode)
        {
            StringBuilder str = new StringBuilder();
            str.Append("UPDATE tbDepartInfo SET DepartState=" + mode.DepartState + " WHERE DepartId=" + mode.DepartId + " ");
            int i = SqlDBhelper.ExecuteNonQuery(str.ToString());
            return i;
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int DepartInfoUpd(ZX.HIS.Model.DepartInfo mode)
        {
            StringBuilder str = new StringBuilder();
            str.AppendLine("UPDATE tbDepartInfo SET");
            str.AppendLine("DepartName=@DepartName");
            str.AppendLine(" WHERE DepartId=@DepartId");
            SqlParameter[] parameters ={
                                       new SqlParameter("@DepartName",SqlDbType.VarChar),
                                       new SqlParameter("@DepartId",SqlDbType.Int)
                                     };
            parameters[0].Value = mode.DepartName;
            parameters[1].Value = mode.DepartId;
            return SqlDBhelper.ExecuteNonQuery(str.ToString(), parameters);
        }

        /// <summary>
        /// 所属部门的所有显示
        /// </summary>
        /// <returns></returns>
        public List<ZX.HIS.Model.DepartInfo> DepartInfoList(int HospitalId)
        {
            StringBuilder SQL = new StringBuilder();
            SQL.AppendLine(" SELECT DepartName,DepartId");
            SQL.AppendLine(" FROM tbDepartInfo WHERE 1=1");
            if (HospitalId != 0)
            {
                SQL.AppendLine(" AND HospitalId=" + HospitalId + "");
            }
            return SqlDBhelper.GetList<ZX.HIS.Model.DepartInfo>(SQL.ToString());
        }
        #endregion

    }

    #region  ExtensionMethod
    ///// <summary>
    ///// 更新一条数据
    ///// </summary>
    //public bool Update(ZX.HIS.Model.StockHist model)
    //{
    //    StringBuilder strSql=new StringBuilder();
    //    strSql.Append("update tbStockHist set ");
    //    strSql.Append("HospitalId=@HospitalId,");
    //    strSql.Append("DrugWareId=@DrugWareId,");
    //    strSql.Append("UserId=@UserId,");
    //    strSql.Append("StockHistNum=@StockHistNum,");
    //    strSql.Append("StockHistDate=@StockHistDate,");
    //    strSql.Append("StockHistPrice=@StockHistPrice");
    //    strSql.Append(" where StockHistId=@StockHistId");
    //    SqlParameter[] parameters = {
    //            new SqlParameter("@HospitalId", SqlDbType.Int,4),
    //            new SqlParameter("@DrugWareId", SqlDbType.Int,4),
    //            new SqlParameter("@UserId", SqlDbType.Int,4),
    //            new SqlParameter("@StockHistNum", SqlDbType.Int,4),
    //            new SqlParameter("@StockHistDate", SqlDbType.DateTime),
    //            new SqlParameter("@StockHistPrice", SqlDbType.Decimal,9),
    //            new SqlParameter("@StockHistId", SqlDbType.Int,4)};
    //    parameters[0].Value = model.HospitalId;
    //    parameters[1].Value = model.DrugWareId;
    //    parameters[2].Value = model.UserId;
    //    parameters[3].Value = model.StockHistNum;
    //    parameters[4].Value = model.StockHistDate;
    //    parameters[5].Value = model.StockHistPrice;
    //    parameters[6].Value = model.StockHistId;

    //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
    //    if (rows > 0)
    //    {
    //        return true;
    //    }
    //    else
    //    {
    //        return false;
    //    }
    //}

    ///// <summary>
    ///// 删除一条数据
    ///// </summary>
    //public bool Delete(int StockHistId)
    //{

    //    StringBuilder strSql=new StringBuilder();
    //    strSql.Append("delete from tbStockHist ");
    //    strSql.Append(" where StockHistId=@StockHistId");
    //    SqlParameter[] parameters = {
    //            new SqlParameter("@StockHistId", SqlDbType.Int,4)
    //    };
    //    parameters[0].Value = StockHistId;

    //    int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
    //    if (rows > 0)
    //    {
    //        return true;
    //    }
    //    else
    //    {
    //        return false;
    //    }
    //}
    #endregion  ExtensionMethod
}

